const PORT_NAME_EXTENSION_PAGE = "port_name_extension_page";
const MSG_TYPE_INIT = "msg_type_init";
const MSG_TYPE_DUMP_DATA = "msg_type_dump_data";
const MSG_TYPE_LOG = "msg_type_log";
const MSG_TYPE_SET_SAMPLE = "msg_type_set_sample";
const MSG_TYPE_SET_FREQ = "msg_type_set_freq";
const MSG_TYPE_START_CSPAT = "msg_type_start_cspat";
const MSG_TYPE_STOP_CSPAT = "msg_type_stop_cspat";
const MSG_TYPE_GET_FOCUS = "msg_type_get_focus";
	 
function cspat_port(cspat) {

	var _cspat = cspat;
	var _bgPort;
	var _that = this;

	this.post_msg = function(msg) {
		_bgPort.postMessage( {type: msg} );
	}

	function init() {
		// init port
		_bgPort = chrome.runtime.connect( {name: PORT_NAME_EXTENSION_PAGE} );
		_that.post_msg(MSG_TYPE_INIT);
		// listen for event
		_bgPort.onMessage.addListener(function(msg) {
			switch(msg.type) {
				case MSG_TYPE_DUMP_DATA:
					cspat.render_manipulation(msg.data);
					break;
				case MSG_TYPE_GET_FOCUS:
					alert("Already Opened.");
				default:
					break;
			}
		});
	}

	init();
}
